/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.device.modular.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import vip.xiaonuo.device.core.enums.Constants;
import vip.xiaonuo.device.core.enums.DeviceErrEnum;
import vip.xiaonuo.device.modular.entity.ApiLog;
import vip.xiaonuo.device.modular.entity.BizCabinetDoor;
import vip.xiaonuo.device.modular.mapper.CabinetDoorMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备api日志Service接口实现类
 *
 * @author levin
 * @date 2022/11/30 13:27
 **/
@Service("deviceDoorCloseService")
public class DeviceDoorCloseServiceImpl extends DeviceBaseService {
    @Override
    protected Integer getReqType() {
        return Constants.CLOSEDOOR;
    }

    @Resource
    private CabinetDoorMapper cabinetDoorMapper;

    @Override
    protected void handleLog(ApiLog log, JSONObject param) {
        JSONObject data = param.getJSONObject("data");
        String cabinetCode = data.getString("cabinetcode");
        String doorcode = data.getString("doorcode");
        List<String> rfids = data.getObject("rfids", List.class);

        log.setCabinetCode(cabinetCode);
        log.setCabinetDoorCode(doorcode);
        log.setRfids(String.join(",", rfids));

        ApiLog latestOpenDoor = apiLogMapper.getLatestOpenDoor(cabinetCode,doorcode);
        //默认不对称
        log.setPariFlag(0);
        if (latestOpenDoor != null) {
            //区间集合-上一次开门
            List<ApiLog> relatedList = apiLogMapper.getDoorRelatedList(cabinetCode, doorcode, latestOpenDoor.getEventTime(), log.getEventTime());
            if (CollectionUtil.isNotEmpty(relatedList)) {
                log.setRelationIds(relatedList.stream().map(ApiLog::getId).collect(Collectors.joining(",")));
                List<ApiLog> closeLog = relatedList.stream().filter(apilog -> apilog.getReqType() == Constants.CLOSEDOOR).collect(Collectors.toList());
                if (closeLog.size() == 0) {
                    //配对-无关门记录
                    log.setRelationId(latestOpenDoor.getId());
                    log.setUserId(latestOpenDoor.getUserId());
                    log.setUserNo(latestOpenDoor.getUserNo());
                    log.setAdminId(latestOpenDoor.getAdminId());
                    log.setPariFlag(1);
                } else {
                    //非配对-有关门记录
                    log.setRelationId(closeLog.get(closeLog.size() - 1).getId());
                }
            }
        }
        //获取柜门信息
        BizCabinetDoor cabinetDoor = cabinetDoorMapper.getInfoByCodes(cabinetCode, doorcode);
        log.setActCompTime(cabinetDoor.getActTime());
        //更新柜门动作时间
        int count = cabinetDoorMapper.updateActTime(cabinetDoor.getId(),log.getEventTime());
        if(count==0){
            log.setResulltType(DeviceErrEnum.REQ_ACT_TIMEOUT.getValue());
        }else{
            log.setResulltType(DeviceErrEnum.SUCCESS.getValue());
        }
    }
}
